package com.yztc.love_travel.ui.mainservice.scenerystrategy.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yztc.love_travel.R;
import com.yztc.love_travel.adater.SceneryStrategyAdapter;
import com.yztc.love_travel.bean.SceneryStrategyBean;
import com.yztc.love_travel.constant.OtherConstant;
import com.yztc.love_travel.constant.URLConstant;
import com.yztc.love_travel.ui.mainservice.scenerystrategy.presenter.SceneryStrategyPresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * "景点攻略"
 */
public class SceneryStrategyActivity extends AppCompatActivity implements ISceneryStrategyView {

    @BindView(R.id.scenerystrategy_toolbar)
    Toolbar scenerystrategyToolbar;
    @BindView(R.id.scenerystrategy_history)
    ListView scenerystrategyHistory;
    @BindView(R.id.activity_scenart_stategy)
    LinearLayout activityScenartStategy;
    private List<SceneryStrategyBean.ManualArticleListBean> mSceneryStrategylist;
    private SceneryStrategyAdapter mSceneryStrategyAdapter;
    private SceneryStrategyPresenterImpl mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenart_stategy);
        ButterKnife.bind(this);
        initPresenter();
        loadData();
        initToolBar();
        initAdapter();
    }

    private void initAdapter() {
        mSceneryStrategylist=new ArrayList<>();
        mSceneryStrategyAdapter=new SceneryStrategyAdapter(mSceneryStrategylist,this);
        scenerystrategyHistory.setAdapter(mSceneryStrategyAdapter);
        scenerystrategyHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(SceneryStrategyActivity.this, SceneryStrategyDetailActivity.class);
                int idl =mSceneryStrategylist.get(position).getArticleId();
                intent.putExtra(OtherConstant.KEY_ARTICAL_SCENERY_ID,idl);
                startActivity(intent);
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    private void initToolBar() {
        scenerystrategyToolbar.setTitle(R.string.scenery_strategy);
        setSupportActionBar(scenerystrategyToolbar);
        ActionBar action=getSupportActionBar();
        action.setDisplayHomeAsUpEnabled(true);

    }

    private void loadData() {
        Map<String,Object> params=new HashMap<>();
        params.put(URLConstant.SceneryStrategy.PAGENO,0);
        params.put(URLConstant.SceneryStrategy.PAGESIZE,20);
        params.put(URLConstant.SceneryStrategy.STATUS,1);
        mPresenter.getSceneryStrategyBean(URLConstant.SceneryStrategy.URL_SCENERYATRATEGY_BASE,params);
    }

    private void initPresenter() {
        mPresenter=new SceneryStrategyPresenterImpl(this);
    }

    @Override
    public void loadStart() {

    }

    @Override
    public void loadFinish() {

    }

    @Override
    public void loadError() {

    }

    @Override
    public void showSceneryStrategyData(SceneryStrategyBean SceneryStrategyBean) {
        List<com.yztc.love_travel.bean.SceneryStrategyBean.ManualArticleListBean> manualArticleList = SceneryStrategyBean.getManualArticleList();
        mSceneryStrategylist.addAll(manualArticleList);
        mSceneryStrategyAdapter.notifyDataSetChanged();
    }
}
